<%--
  Created by IntelliJ IDEA.
  User: oldliu
  Date: 2023-04-07
  Time: 10:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>cookie记录此网页访问次数</title>
</head>
<body>
  <%
      //一个cookie是一对键值对
      Cookie[] cookies = request.getCookies();
      //visit_num
      Cookie v=null;
      for (int i = 0;cookies!=null&& i < cookies.length; i++) {
          if(cookies[i].getName().equals("visit_num")){
              v=cookies[i];
              break;
          }
      }
      if(v==null){
          v=new Cookie("visit_num","1");
      }else{
          int k=Integer.parseInt(v.getValue())+1;
          v.setValue(String.valueOf(k));
      }
      v.setMaxAge(86400*3);//3天
      response.addCookie(v);//发给浏览器
  %>
<h1>当前页您已经访问了<%=v.getValue()%>次</h1>
</body>
</html>
